#include <assert.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <ctime>
#include <set>
#include <vector>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class Hex
{
public:
	unsigned int b0:4;
	unsigned int b1:4;
	unsigned int b2:4;
	unsigned int b3:4;
	unsigned int b4:4;
	unsigned int b5:4;
	unsigned int b6:4;
	unsigned int b7:4;
	bool operator<(const Hex& rhs) const;
	bool operator==(const Hex& rhs) const;
};
class Guid
{
public:
	Hex number[4];
	bool operator<(const Guid& rhs) const;
};
void output_hex(std::ostream& s, int a)
{
	assert(a < 16);
	assert(a > -1);
	if (a > 9) {
		char ch('A');
		ch += a - 10;
		s << ch;	
	} else
		s << a;
}
std::ostream& operator<<(std::ostream& s, const Hex& h)
{
	output_hex(s, h.b0);
	output_hex(s, h.b1);
	output_hex(s, h.b2);
	output_hex(s, h.b3);
	output_hex(s, h.b4);
	output_hex(s, h.b5);
	output_hex(s, h.b6);
	output_hex(s, h.b7);
	return s;
}
std::ostream& operator<<(std::ostream& s, const Guid& guid)
{
	for(int i = 0; i < 4; ++i)
		s << guid.number[i];
	return s;
}	
bool Hex::operator==(const Hex& rhs) const
{
	return b0 == rhs.b0 && b1 == rhs.b1 && b2 == rhs.b2 && b3 == rhs.b3 && b4 == rhs.b4 &&
		b5 == rhs.b5 && b6 == rhs.b6 && b7 == rhs.b7;
}
bool Hex::operator<(const Hex& rhs) const
{
	if (b0 < rhs.b0) 
		return true;
	else if (b0 > rhs.b0)
		return false;
	if (b1 < rhs.b1) 
		return true;
	else if (b1 > rhs.b1)
		return false;	
	if (b2 < rhs.b2) 
		return true;
	else if (b2 > rhs.b2)
		return false;
	if (b3 < rhs.b3) 
		return true;
	else if (b3 > rhs.b3)
		return false;
	if (b4 < rhs.b4) 
		return true;
	else if (b4 > rhs.b4)
		return false;
	if (b5 < rhs.b5) 
		return true;
	else if (b5 > rhs.b5)
		return false;
	if (b6 < rhs.b6) 
		return true;
	else if (b6 > rhs.b6)
		return false;
	if (b7 < rhs.b7) 
		return true;
	return false;
}
bool Guid::operator<(const Guid& rhs) const
{
	for(int i = 0; i < 4; ++i)
	{
		const Hex& n = number[i];
		if (n == rhs.number[i])
			continue;
		return n < rhs.number[i];
	}
	return false;
}
int main(int argc, char **argv)
{
#ifdef _DEBUG
	std::cout << "debug" << std::endl;
#endif
#ifdef NDEBUG
	std::cout << "release" << std::endl;
#endif
	std::srand(0);
	boost::posix_time::ptime time = boost::posix_time::second_clock::local_time();
	std::vector<Guid> data;
	for(int i = 0; i < 10; ++i) {
		Guid guid;
		for(int i = 0; i < 4; ++i) {
			int* a = reinterpret_cast<int*>(&guid.number[i]);
		       	*a = rand();
		}
		data.push_back(guid);
	}
	std::sort(data.begin(), data.end(), boost::bind(std::less<Guid>(), _1, _2));
	for(int i = 0; i < data.size(); ++i) {
		std::cout << "0x" << data.at(i) << std::endl;
	}
	boost::posix_time::time_duration duration = boost::posix_time::second_clock::local_time() - time;
	std::cout << "cost " << duration << std::endl;
	return 0;
}
